package com.example.bookdemo.controller;
import com.example.bookdemo.model.*;
import com.example.bookdemo.service.bookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j

@RequestMapping("/book")
@RestController
public class bookController {
    @Autowired
    private bookService bookService;

    public List<bookInfo> getList(){
        return bookService.getList();
    }

    @RequestMapping("/getList")
    public Result<PageResult<bookInfo>> getListByPage(RequestPage requestPage, HttpSession session){
        //1.判断是否登录,没登陆就不会返回任何信息(后端),前端进行页面跳转
        PageResult<bookInfo> bookInfoPageResult = bookService.getListByPage(requestPage);
        return Result.success(bookInfoPageResult);
    }


    @RequestMapping("/addBook")
    public Boolean addBook(bookInfo bookInfo){
        //判断参数
        if(!StringUtils.hasLength(bookInfo.getBookName())||
        !StringUtils.hasLength(bookInfo.getAuthor())||
        !StringUtils.hasLength(bookInfo.getPublish())||
        bookInfo.getPrice()==null||
        bookInfo.getCount()==null){
            //信息不全
            return false;
        }
        Integer result = bookService.insertBook(bookInfo);
        if (result==0) {
            //没有添加成功
            return false;
        }
        return true;
    }


    //修改信息(先查到对应的,在进行修改)
    //查找
    @RequestMapping("/getBookInfo")
    public bookInfo getBookById(Integer bookId){
        if(bookId==null){
            return null;
        }
        return bookService.selectBookById(bookId);
    }

    //修改
    @RequestMapping("/updateBook")
    public Boolean updateBookInfo(bookInfo bookInfo){
        if(bookInfo.getId()==null){
            return false;
        }
        if(bookInfo.getBookName()!=null&&"".equals(bookInfo.getBookName().trim())){
            return false;
        }
        Integer result = bookService.updateBookInfoById(bookInfo);
        try {
            if(result>0){
                return true;
            }
            return false;
        }catch (Exception e){
            //输出日志
            log.error("updateBookInfo errer, e: {}",e);
            return false;
        }
    }

    //删除
    @RequestMapping("/deleteBook")
    public Boolean deleteBookInfoById(Integer id){
        if (id==null)
        {
            return false;
        }
        Integer result = bookService.deleteBookINfoById(id);
        try {
            return result>0;
        }catch (Exception e){
            log.error("deleteBookInfoById error,e:{}",e);
            return false;
        }
    }


    //批量删除
    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids){
        if(ids.size()==0||ids==null){
            return true;
        }
        Integer result = bookService.batchDelete(ids);
        try {
            if(result>0){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("deleteBookInfoById error,e:{}",e);
            return false;
        }
    }
}
